package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.*;
import com.wangdao.wechatmall.bean.pojo.*;
import com.wangdao.wechatmall.bean.vo.admin.AdCreateVO;
import com.wangdao.wechatmall.bean.vo.admin.AdListVo;
import com.wangdao.wechatmall.bean.vo.admin.BaseDataVO;
import com.wangdao.wechatmall.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-09 15:09
 */
@Service
public class PromotionServiceImpl implements PromotionService {
    @Autowired
    AdMapper adMapper;

    /**
     * 实现对ad/list接口数据的操作
     * @param baseReqBO
     * @return
     */
    @Override
    public BaseDataVO adList(BaseReqBO baseReqBO) {
        AdListBO adListBO = (AdListBO) baseReqBO;

        PageHelper.startPage(adListBO.getPage(), adListBO.getLimit());

        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        if (adListBO.getContent() != null) {
            criteria.andContentLike("%" + adListBO.getContent() + "%");
        }
        if (adListBO.getName() != null) {
            criteria.andNameLike("%" + adListBO.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);
        adExample.setOrderByClause(adListBO.getSort() + " " + adListBO.getOrder());
        List<Ad> ads = adMapper.selectByExample(adExample);
        PageInfo<Ad> adPageInfo = new PageInfo<>(ads);
        long total = adPageInfo.getTotal();
        List<AdListVo> adListVos = new ArrayList<>();
        for (Ad ad : ads) {
            AdListVo adListVo = new AdListVo();
            adListVo.setId(ad.getId());
            adListVo.setName(ad.getName());
            adListVo.setLink(ad.getLink());
            adListVo.setUrl(ad.getUrl());
            adListVo.setPosition(ad.getPosition());
            adListVo.setContent(ad.getContent());
            adListVo.setEnabled(ad.getEnabled());
            adListVo.setAddTime(ad.getAddTime());
            adListVo.setUpdateTime(ad.getUpdateTime());
            adListVo.setDeleted(ad.getDeleted());
            adListVos.add(adListVo);
        }
        return BaseDataVO.ok(adListVos, total);
    }

//    @Autowired
//    StorageMapper storageMapper;
//    @Value("D:\\download/")
//    String filePath;
//    @Value("http://localhost:8083")
//    String localhost;
//    @Value("/wx/storage/fetch/")
//    String relativePath;
//    /**
//     * 实现文件上传
//     * @param file
//     * @return
//     */
//    @Override
//    public StorageCreateVO storageCreate(MultipartFile file) {
//        String originalFilename = file.getOriginalFilename();
//        String contentType = file.getContentType();
//        Integer size = (int) file.getSize();
//
//        String uuid = UUID.randomUUID().toString();
//        int index = originalFilename.lastIndexOf(".");
//        String substring = originalFilename.substring(index);
//        String fileName = uuid + substring;
//
//        File newFile = new File(filePath, fileName);
//        if (!newFile.getParentFile().exists()) {
//            newFile.getParentFile().mkdirs();
//        }
//        try {
//            file.transferTo(newFile);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        String url = localhost + filePath + fileName;
//
//        // 扔到数据库里面
//        Storage storage = new Storage();
//        storage.setKey(fileName);
//        storage.setName(originalFilename);
//        storage.setType(contentType);
//        storage.setSize(size);
//        storage.setUrl(url);
//        storage.setAddTime(new Date());
//        storage.setUpdateTime(new Date());
//        int affect = storageMapper.insertSelective(storage);
//        StorageCreateVO storageCreateVO = new StorageCreateVO();
//        storageCreateVO.setId(storage.getId());
//        storageCreateVO.setKey(fileName);
//        storageCreateVO.setName(originalFilename);
//        storageCreateVO.setType(contentType);
//        storageCreateVO.setSize(size);
//        storageCreateVO.setUrl(url);
//        storageCreateVO.setAddTime(new Date());
//        storageCreateVO.setUpdateTime(new Date());
//
//
//        return storageCreateVO;
//    }

    @Override
    public void adDelete(Ad ad) {
        ad.setDeleted(true);
        int effect = adMapper.updateByPrimaryKeySelective(ad);
    }

    @Override
    public AdCreateVO adUpdate(Ad ad) {
        int effectRows = adMapper.updateByPrimaryKeySelective(ad);
        AdCreateVO adCreateVO = new AdCreateVO();
        adCreateVO.setId(ad.getId());
        adCreateVO.setName(ad.getName());
        adCreateVO.setLink(ad.getLink());
        adCreateVO.setUrl(ad.getUrl());
        adCreateVO.setPosition(ad.getPosition());
        adCreateVO.setContent(ad.getContent());
        adCreateVO.setEnabled(ad.getEnabled());
        adCreateVO.setAddTime(ad.getAddTime());
        adCreateVO.setUpdateTime(ad.getUpdateTime());
        return adCreateVO;
    }

    @Override
    public AdCreateVO adCreate(Ad ad) {
        ad.setAddTime(new Date());
        ad.setUpdateTime(new Date());
        int effect = adMapper.insertSelective(ad);
        AdCreateVO adCreateVO = new AdCreateVO();
        adCreateVO.setId(ad.getId());
        adCreateVO.setName(ad.getName());
        adCreateVO.setLink(ad.getLink());
        adCreateVO.setUrl(ad.getUrl());
        adCreateVO.setPosition(ad.getPosition());
        adCreateVO.setContent(ad.getContent());
        adCreateVO.setEnabled(ad.getEnabled());
        adCreateVO.setAddTime(ad.getAddTime());
        adCreateVO.setUpdateTime(ad.getUpdateTime());
        return adCreateVO;
    }


    /*--------------------------------------*/
    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;
    @Override
    public BaseDataVO couponList(CouponListBO couponListBO) {
        PageHelper.startPage(couponListBO.getPage(),couponListBO.getLimit());
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        if (couponListBO.getName() != null) {
            criteria.andNameLike("%" + couponListBO.getName() + "%");
        }
        if (couponListBO.getType() != null) {
            criteria.andTypeEqualTo(couponListBO.getType());
        }
        if (couponListBO.getStatus() != null) {
            criteria.andStatusEqualTo(couponListBO.getStatus());
        }
        criteria.andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();
        couponExample.setOrderByClause(couponListBO.getOrder() + " " + couponListBO.getSort());
        return BaseDataVO.ok(coupons, total);

        /*
        criteria.andDeletedEqualTo(false);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        //查询总数
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);
        long total = couponPageInfo.getTotal();
        //去掉start_time,end_time,新建adListVos
        List<CouponListVo> couponListVos = new ArrayList<>();
        for (Coupon coupon : coupons) {
            CouponListVo couponListVo = new CouponListVo();
            couponListVo.setId(coupon.getId());
            couponListVo.setName(coupon.getName());
            couponListVo.setDesc(coupon.getDesc());
            couponListVo.setTag(coupon.getTag());
            couponListVo.setTotal(coupon.getTotal());
            couponListVo.setDiscount(coupon.getDiscount());
            couponListVo.setMin(coupon.getMin());
            couponListVo.setType(coupon.getType());
            couponListVo.setStatus(coupon.getStatus());
            couponListVo.setGoodsType(coupon.getGoodsType());
            couponListVo.setGoodsValue(coupon.getGoodsValue());
            couponListVo.setTimeType(coupon.getTimeType());
            couponListVo.setDays(coupon.getDays());
            couponListVo.setAddTime(coupon.getAddTime());
            couponListVo.setUpdateTime(coupon.getUpdateTime());
            couponListVo.setDeleted(coupon.getDeleted());
            couponListVos.add(couponListVo);
        }
        return BaseData.ok(couponListVos,total);


         */

    }

    @Override
    public Coupon couponCreate(Coupon coupon) {
        coupon.setAddTime(new Date());
        coupon.setUpdateTime(new Date());
        int affect = couponMapper.insertSelective(coupon);
        return coupon;
    }

    @Override
    public Coupon couponRead(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public BaseDataVO couponListUser(CouponListUserBO couponListUserBO) {
        PageHelper.startPage(couponListUserBO.getPage(), couponListUserBO.getLimit());
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

        if (couponListUserBO.getCouponId() != null) {
            criteria.andCouponIdEqualTo(couponListUserBO.getCouponId());
        }
        if (couponListUserBO.getUserId() != null) {
            criteria.andUserIdEqualTo(couponListUserBO.getUserId());
        }
        if (couponListUserBO.getStatus() != null) {
            criteria.andStatusEqualTo(couponListUserBO.getStatus());
        }
        criteria.andDeletedEqualTo(false);
        couponUserExample.setOrderByClause(couponListUserBO.getSort() + " " + couponListUserBO.getOrder());
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUsers);
        long total = couponUserPageInfo.getTotal();
        return BaseDataVO.ok(couponUsers, total);
    }

    @Override
    public Coupon couponUpdate(CouponUpdateBO couponUpdateBO) {
        Coupon coupon = new Coupon();
        coupon.setId(couponUpdateBO.getId());
        coupon.setName(couponUpdateBO.getName());
        coupon.setDesc(couponUpdateBO.getDesc());
        coupon.setTag(couponUpdateBO.getTag());
        coupon.setTotal(couponUpdateBO.getTotal());
        coupon.setDiscount(couponUpdateBO.getDiscount());
        coupon.setMin(couponUpdateBO.getMin());
        coupon.setLimit(couponUpdateBO.getLimit());
        coupon.setType(couponUpdateBO.getType());
        coupon.setStatus(couponUpdateBO.getStatus());
        coupon.setGoodsType(couponUpdateBO.getGoodsType());
        coupon.setGoodsValue(couponUpdateBO.getGoodsValue());
        coupon.setTimeType(couponUpdateBO.getTimeType());
        coupon.setDays(couponUpdateBO.getDays());
        coupon.setStartTime(couponUpdateBO.getStartTime());
        coupon.setEndTime(couponUpdateBO.getEndTime());
        coupon.setAddTime(couponUpdateBO.getAddTime());
        coupon.setUpdateTime(couponUpdateBO.getUpdateTime());
        coupon.setDeleted(couponUpdateBO.getDeleted());
        //
        int affect = couponMapper.updateByPrimaryKeySelective(coupon);


        return coupon;
    }

    @Override
    public void couponDelete(CouponDeleteBO couponDeleteBO) {
        //封装对象
        Coupon coupon = new Coupon();
        coupon.setId(couponDeleteBO.getId());
        coupon.setDeleted(true);
        //
        int affect = couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Autowired
    TopicMapper topicMapper;

    @Override
    public BaseDataVO topicList(TopicListBO topicListBO) {
        //分页
        PageHelper.startPage(topicListBO.getPage(),topicListBO.getLimit());

        TopicExample topicExample = new TopicExample();

        topicExample.setOrderByClause(topicListBO.getSort() + " " + topicListBO.getOrder());
        TopicExample.Criteria criteria = topicExample.createCriteria();

        if (topicListBO.getTitle() != null) {
            criteria.andTitleLike("%" + topicListBO.getTitle()+ "%");
        }
        if (topicListBO.getSubtitle() != null) {
            criteria.andSubtitleLike("%"+ topicListBO.getSubtitle()+"%");
        }
        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);

        //查询总数
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();
        return BaseDataVO.ok(topics, total);
    }

    @Override
    public Topic topicCreate(Topic topic) {
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        int i = topicMapper.insertSelective(topic);
        return topic;
    }

    @Override
    public Topic topicUpdate(Topic topic) {
        topic.setUpdateTime(new Date());
        int affect = topicMapper.updateByPrimaryKeySelective(topic);
        return topic;
    }

    @Override
    public void topicDelete(Topic topic) {
        topic.setDeleted(true);
        int affect = topicMapper.updateByPrimaryKeySelective(topic);
    }
}
